package com.example.jqying.androiddemo.recyclerView.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jqying.androiddemo.R;

import java.util.List;

/**
 * Created by apple on 2018/6/23.
 */

public class ViewHolerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {



    private Context mContext;
    private List<String > mList;
    private OnitemClickListener mlistener;


    /*
    *   上接LinearRecycleActivity
    *   使用流程
    *   1。构造时传入Context上下文
    *   2。设置RecycleView中item的数量
    *   3。设置每个Item的layout，通过LinearViewHolder
    *   4。linearViewHolder中的layout绑定到RecycleView中
    *   5。通过onBindView设置layout内部控件的属性
     */

    public ViewHolerAdapter(Context context, OnitemClickListener listener){
        mContext = context;
        this.mlistener = listener;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==0) {
            return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item, parent, false));
        }else if(viewType==1)
        {
            return new LinearViewHolder2(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item_2, parent, false));
        }
        else return  null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if(getItemViewType(position)==0){
            if(((LinearViewHolder)holder).textView!=null) {
                ((LinearViewHolder)holder).textView.setText("2018世界杯德国补时阶段绝杀！！！");

            }
        }else{
            if(((LinearViewHolder2)holder).textView!=null) {
                ((LinearViewHolder2)holder).textView.setText("Code Blue");

                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529842292646&di=3adf248a6ba217c35b3d14728b95cf87&imgtype=0&src=http%3A%2F%2Ff9.topitme.com%2F9%2F87%2Fce%2F1124870400d0dce879o.jpg").
                        into(((LinearViewHolder2)holder).imageView);

            }

        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //position 为触发事件的Item的编号
                //Toast.makeText(mContext,"Clicked : "+ position,Toast.LENGTH_SHORT).show();

                //调用用户定义的事件处理函数，类似C#的订阅和发布关系
                mlistener.onClick(position);
            }
        });

        //也可以处理其它事件，也可运用低耦合的接口
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                return false;
            }
        });


    }


    //返回Item使用的Layout 编号
    @Override
    public int getItemViewType(int position) {
        if(position%2!=0) {
            return 0;
        }
        else{
            return 1;

        }
        //return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return 30;
    }


    class LinearViewHolder extends RecyclerView.ViewHolder{

        private TextView textView;

        public LinearViewHolder(View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.tv_title);
        }
    }
    class LinearViewHolder2 extends RecyclerView.ViewHolder{

        private TextView textView;
        private ImageView imageView;

        public LinearViewHolder2(View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.tv_title);
            imageView = (ImageView)itemView.findViewById(R.id.iv_test);
        }
    }


    /*
    *   定义一个interface用于传入用户定义的事件
     */

    public interface OnitemClickListener{
        void onClick(int pos);
    }
}
